﻿Imports System.Reflection

''' <summary>
''' 列属性読み込みクラス
''' </summary>
''' <remarks>取得の際には TableAttributeReader を使用しています</remarks>
Public Class ColumnAttributeReader

    ''' <summary>
    ''' 列属性を取得します
    ''' </summary>
    ''' <param name="t"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Read(ByVal t As Type) As IEnumerable(Of ColumnAttribute)
#If DEBUG Then
        If t Is Nothing Then
            Throw New ArgumentException("省略できません。", "t")
        End If
#End If

        'テーブル属性リーダーがキャッシュを持っているため、そちら経由での読み込みにしました。
        Return t.ToTable.Columns
    End Function

    ''' <summary>
    ''' 列属性を取得します
    ''' </summary>
    ''' <param name="member"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Read(ByVal member As MemberInfo) As IEnumerable(Of ColumnAttribute)
#If DEBUG Then
        If member Is Nothing Then
            Throw New ArgumentException("省略できません。", "prop")
        End If
#End If

        Return Read(DirectCast(member, PropertyInfo))
    End Function

    ''' <summary>
    ''' 列属性を取得します
    ''' </summary>
    ''' <param name="prop"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Read(ByVal prop As PropertyInfo) As IEnumerable(Of ColumnAttribute)
#If DEBUG Then
        If prop Is Nothing Then
            Throw New ArgumentException("省略できません。", "prop")
        End If
#End If

        Return (From el In Read(prop.DeclaringType) Where el.PropertyInfo.Equals(prop))
    End Function

    Public Shared Function ReadPrimaryKeys(t As Type) As IEnumerable(Of ColumnAttribute)
#If DEBUG Then
        If t Is Nothing Then
            Throw New ArgumentException("省略できません。", "t")
        End If
#End If

        Return (From el In Read(t) Where el.IsPrimaryKey)
    End Function

    Public Shared Function ReadParentRelationColumns(ByVal prop As PropertyInfo) As IEnumerable(Of ParentRelationColumnAttribute)
#If DEBUG Then
        If prop Is Nothing Then
            Throw New ArgumentException("省略できません。", "prop")
        End If
#End If

        Return (From el In Read(prop) Where el.IsParent Select DirectCast(el, ParentRelationColumnAttribute))
    End Function

End Class
